package com.rudolfheszele.smsselector.model;

public class SmsSelectorRuleSetEvaluationEngine
{
	public static boolean isRuleSetMatch(SmsSelectorRuleSet ruleSet, SmsSelectorSmsMessage smsMessage)
	{
		boolean returnValue = false;
		
		do
		{
			if(ruleSet.getRules().size() == 1)
			{
				returnValue = ruleSet.getRules().get(0).isRuleMatch(smsMessage);
				break;
			}
			if(ruleSet.getRules().size() > 1)
			{
				boolean valueA = ruleSet.getRules().get(0).isRuleMatch(smsMessage);
				boolean valueB = ruleSet.getRules().get(1).isRuleMatch(smsMessage);
				boolean valueC = ruleSet.getLogicalOperations().get(0).doOperation(valueA, valueB);
				
				for(int i = 2; i < ruleSet.getRules().size(); i++)
				{
					valueA = valueC;
					valueB =  ruleSet.getRules().get(i).isRuleMatch(smsMessage);
					valueC = ruleSet.getLogicalOperations().get(i - 1).doOperation(valueA, valueB);
				}
				returnValue = valueC;
			}
		}
		while(false);
		if(returnValue == true)
			smsMessage.setAppliedRuleSet(ruleSet);
		
		return returnValue;
	}
}
